#!/usr/bin/env python3

# Copyright (c) 2005-2014 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


from ...._plugin import KernelAddonPlugin


class Plugin(KernelAddonPlugin):

    def modify_kernel_config(self, context):
        bBcache = False
        bExt4 = False
        bBtrfs = False
        bBcachefs = False
        bSquashfs = False
        bIsofs = False
        bNtfs = False
        bFat = False
        if "efi-bcachefs" in self.params:
            bFat = True
            bBcachefs = True
        if "efi-bcache-btrfs" in self.params:
            bFat = True
            bBcache = True
            bBtrfs = True
        if "efi-btrfs" in self.params:
            bFat = True
            bBtrfs = True
        if "efi-ext4" in self.params:
            bFat = True
            bExt4 = True
        if "efi-msr-ntfs" in self.params:
            bFat = True
            bNtfs = True
        if "bios-ext4" in self.params:
            bExt4 = True
        if "bios-ntfs" in self.params:
            bNtfs = True
        if "bios-fat" in self.params:
            bFat = True
        if "squashfs-on-isofs" in self.params:
            bSquashfs = True
            bIsofs = True
        if "squashfs-on-ntfs" in self.params:
            bSquashfs = True
            bNtfs = True
        if "squashfs-on-fat" in self.params:
            bSquashfs = True
            bFat = True

        if bBcache:
            context.select_by_symbol("BCACHE", "m")

        if bExt4:
            context.select_by_symbol("EXT4_FS", "m")
            context.select_tree_by_symbol("EXT4_FS", "m,y", value_priority=context.ValuePriority.USER_POLICY)

        if bBtrfs:
            context.select_by_symbol("BTRFS_FS", "m")
            context.select_by_symbol("BTRFS_FS_POSIX_ACL", "y")             # there're other debug switches under BTRFS menu, this one is not

        if bBcachefs:
            # FIXME
            assert False

        if bSquashfs:
            context.select_by_symbol("SQUASHFS", "m")
            context.set_choice_by_path(["File systems", "Miscellaneous filesystems", "SquashFS 4.0 - ...", "File decompression options"], "SQUASHFS_FILE_DIRECT")
            context.select_by_symbol("SQUASHFS_4K_DEVBLK_SIZE", "n")
            context.select_by_symbol("SQUASHFS_EMBEDDED", "n")
            context.select_tree_by_symbol("SQUASHFS", "m,y", value_priority=context.ValuePriority.USER_POLICY)

        if bIsofs:
            context.select_by_symbol("ISO9660_FS", "m")
            context.select_tree_by_symbol("ISO9660_FS", "m,y", value_priority=context.ValuePriority.USER_POLICY)
            context.select_by_symbol("UDF_FS", "m")
            context.select_tree_by_symbol("UDF_FS", "m,y", value_priority=context.ValuePriority.USER_POLICY)

        if bNtfs:
            context.select_by_symbol("NTFS3_FS", "m")
            context.select_tree_by_symbol("NTFS3_FS", "m,y", value_priority=context.ValuePriority.USER_POLICY)

        if bFat:
            context.select_by_symbol("VFAT_FS", "m")
            context.select_tree_by_symbol("VFAT_FS", "m,y", value_priority=context.ValuePriority.USER_POLICY)

        if bNtfs or bFat:
            context.select_by_symbol("NLS", "y")
            context.set_string_by_symbol("NLS_DEFAULT", "iso8859-1")
            context.select_tree_by_symbol("NLS", "m", value_priority=context.ValuePriority.USER_POLICY)
